<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>侧边栏渐入渐出效果</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        .sidebar {
            transition: transform 0.3s ease-in-out;
            transform: translateX(-100%);
        }

        .sidebar.open {
            transform: translateX(0);
        }

        .overlay {
            background-color: rgba(0, 0, 0, 0.5);
            transition: opacity 0.3s ease-in-out;
            opacity: 0;
            pointer-events: none;
        }

        .overlay.active {
            opacity: 1;
            pointer-events: auto;
        }
    </style>
</head>

<body class="bg-gray-100 min-h-screen">
    <!-- 主内容区 -->
    <div class="relative">
        <button id="openSidebar"
            class="p-4 bg-blue-500 text-white m-4 rounded-lg shadow-lg hover:bg-blue-600 transition-colors">
            <i class="fa fa-bars mr-2"></i>打开侧边栏
        </button>

        <h1 class="text-2xl font-bold text-center my-8">主内容区域</h1>
    </div>

    <!-- 侧边栏 -->
    <div id="sidebar" class="sidebar fixed top-0 left-0 h-full w-64 bg-white shadow-2xl z-20">
        <div class="p-4 border-b flex justify-between items-center">
            <h2 class="text-xl font-bold">侧边栏</h2>
            <button id="closeSidebar" class="text-gray-500 hover:text-gray-700">
                <i class="fa fa-times text-xl"></i>
            </button>
        </div>

        <div class="p-4">
            <ul class="space-y-4">
                <li><a href="#" class="text-blue-500 hover:text-blue-700 transition-colors">首页</a></li>
                <li><a href="#" class="text-blue-500 hover:text-blue-700 transition-colors">产品</a></li>
                <li><a href="#" class="text-blue-500 hover:text-blue-700 transition-colors">服务</a></li>
                <li><a href="#" class="text-blue-500 hover:text-blue-700 transition-colors">关于我们</a></li>
                <li><a href="#" class="text-blue-500 hover:text-blue-700 transition-colors">联系我们</a></li>
            </ul>
        </div>
    </div>

    <!-- 遮罩层 -->
    <div id="overlay" class="overlay fixed inset-0 z-10"></div>

    <script>
        //实现一个侧边栏，点击按钮可以让其平滑滑入 / 滑出。要求：
        //初始状态侧边栏隐藏在页面左侧
        // 点击 "打开侧边栏" 按钮，侧边栏平滑滑入
        // 点击 "关闭侧边栏" 按钮或侧边栏内的关闭按钮，侧边栏平滑滑出
        // 点击侧边栏外部区域也能关闭侧边栏
        // 补全代码
        // 获取DOM元素
        const openSidebarBtn = document.getElementById('openSidebar');
        const closeSidebarBtn = document.getElementById('closeSidebar');
        const sidebar = document.getElementById('sidebar');
        const overlay = document.getElementById('overlay');

        // 打开侧边栏函数
        function openSidebar() {
            sidebar.classList.add('open');
            overlay.classList.add('active');
        }

        // 关闭侧边栏函数
        function closeSidebar() {
            sidebar.classList.remove('open');
            overlay.classList.remove('active');
        }

        // 为打开按钮添加点击事件监听器
        openSidebarBtn.addEventListener('click', openSidebar);

        // 为关闭按钮添加点击事件监听器
        closeSidebarBtn.addEventListener('click', closeSidebar);

        // 为遮罩层添加点击事件监听器，点击外部区域关闭侧边栏
        overlay.addEventListener('click', closeSidebar);
    </script>
</body>

</html>